package cn.itsource.pet.controller;

import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.service.IShopService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.LoginContext;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IShopService shopService;

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @PostMapping
    public PageResult<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        //获取当前系统的登录用户
        Employee employee= (Employee) LoginContext.getCurrent(request);
        //当前用户对应的店铺
        Shop shop = shopService.loadByAdminId(employee.getId());
        //把店铺的id作为一个查询条件 传入到query里面
        query.setShopId(shop.getId());

        return searchMasterMsgService.queryPage(query);
    }

}
